package com.czy.owner.adapter;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.NearStoreListModel;
import com.czy.owner.entity.SelectStoreModel;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

/**
 * Created by asus on 2017/6/5.
 */

public class SelectStoreAdapter extends RecyclerArrayAdapter<SelectStoreModel> {

    private Context context;
    private boolean hideServicePrice = false;

    public SelectStoreAdapter(Context context) {
        super(context);
        this.context = context;
    }
    public SelectStoreAdapter(Context context, boolean hideServicePrice) {
        super(context);
        this.context = context;
        this.hideServicePrice = hideServicePrice;
    }
    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new SelectStoreListHolder(parent);


    }


    @Override
    public void OnBindViewHolder(BaseViewHolder holder, int position) {
        super.OnBindViewHolder(holder, position);
    }


    class SelectStoreListHolder extends BaseViewHolder<SelectStoreModel> {
        private ImageView imgBindStoreHead;
        private TextView tvStoreName;
        private TextView tvServicePrice;
        private TextView tvStoreAdrr;
        private TextView tvStoreDistance;

        public SelectStoreListHolder(ViewGroup parent) {
            super(parent, R.layout.item_select_store_recycler);
            imgBindStoreHead = $(R.id.img_bind_store_head);
            tvStoreName = $(R.id.tv_store_name);
            tvServicePrice = $(R.id.tv_service_price);
            tvStoreAdrr = $(R.id.tv_store_adrr);
            tvStoreDistance = $(R.id.tv_store_distance);
        }

        @Override
        public void setData(final SelectStoreModel model) {
            super.setData(model);
            GlideUtils.loadImage(context, model.getStroeLogo(), imgBindStoreHead, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
            tvStoreName.setText(model.getStoreName());
            tvStoreAdrr.setText(model.getStoreLocation());
            if (model.getMin() < 1) {
                tvStoreDistance.setText((int) (model.getMin() * 1000) + "米以内");
            } else {
                tvStoreDistance.setText(model.getMin() + "km");
            }
            String strNowPrice = "安装费￥"  + String.format("%.2f", model.getServicePrice()) ;
            SpannableString styledText = new SpannableString(strNowPrice);
            styledText.setSpan(new TextAppearanceSpan(context, R.style.style_red_small), 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new TextAppearanceSpan(context, R.style.style_red_big), 4, styledText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvServicePrice.setText(styledText, TextView.BufferType.SPANNABLE);
            if(hideServicePrice){
                tvServicePrice.setVisibility(View.GONE);
            }


        }


    }


}

